﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demonstartor
{

    public class DateTimeValues
    {
        public DateTime[] X { get; private set; }
        public double[] Y { get; private set; }
        public int Count { get; private set; }
        public double Freq { get; private set; }

        public DateTime StartTime { get; private set; }
        public DateTime EndTime { get; private set; }

        public DateTimeValues(DateTime Start, DateTime End, double Freq, double[] Y)
        {
            this.StartTime = Start;
            this.EndTime = End;
            Count = (int)((End - Start).TotalSeconds * Freq);
            if (Count > Y.Count())
                Count = Y.Count();
            this.Freq = Freq;
            this.X = new DateTime[Count];
            this.Y = new double[Count];
            for (int i = 0; i < Count; i++)
            {
                X[i] = Start.AddSeconds((double)i / Freq);
                this.Y[i] = Y[i];
            }
        }

        public double Min()
        {
            if (Y.Count() == 0)
                return double.PositiveInfinity;
            return Y.Min();
        }

        public double Max()
        {
            if (Y.Count() == 0)
                return double.NegativeInfinity;
            return Y.Max();
        }

        public bool InInterval(DateTime DT)
        {
            return ((DT > StartTime) && (DT < EndTime));
        }

        public DateTimeValues[] Split(DateTime DT)
        {
            DateTimeValues[] Res = new DateTimeValues[2];

            int i = 0;
            DateTime Val = DateTime.Now;
            for (; i < Count; i++)
                if (X[i] > DT)
                {
                    Val = X[i];
                    break;
                }

            if (i == 0)
            {
                Res[0] = new DateTimeValues(DT, DT, 4, new double[0]);
                Res[1] = this;
                return Res;
            }

            if (i == Count)
            {
                Res[1] = new DateTimeValues(DT, DT, 4, new double[0]);
                Res[0] = this;
                return Res;
            }

            double[] LeftVals = new double[i];
            double[] RightVals = new double[Count - i + 1];

            for (int j = 0; j < i; j++)
                LeftVals[j] = Y[j];

            for (int j = i - 1; j < Count; j++)
                RightVals[j - i + 1] = Y[j];

            Res[0] = new DateTimeValues(StartTime, Val, 4, LeftVals);
            Res[1] = new DateTimeValues(Val.AddSeconds(-0.25), EndTime, 4, RightVals);

            return Res;
        }


        public void Normalize(double Min, double Max)
        {
            double Len = Max - Min;
            if (Len != 0)
            {
                for (int i = 0; i < Count; i++)
                {
                    Y[i] = (Y[i] - Min) / Len;
                }
            }
            else
            {
                for (int i = 0; i < Count; i++)
                {
                    if (Max >= 1)
                        Max = 1;
                    if (Max <= 0)
                        Max = 0;
                    Y[i] = Max;
                }
            }
        }
    }

}
